﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zhaoxi.AgiletyFramework.DbModels.Models;

namespace Zhaoxi.AgiletyFramework.DbModels
{
    public class AgiletyDbContext : DbContext
    {
        /// <summary>
        /// 数据库连接字符串
        /// </summary>
        private string ConnectionString;

        /// <summary>
        /// 带参数的构造函数
        /// </summary>
        /// <param name="connectionString">数据库连接字符串</param>
        public AgiletyDbContext(string connectionString)
        {
            ConnectionString = connectionString;
        }

        public AgiletyDbContext(DbContextOptions<AgiletyDbContext> options)
            : base(options)
        {
        }

        // 变y为i加es
        public virtual DbSet<UserEntity> UserEntities { get; set; }

        /// <summary>
        /// 配置DbContext的参数
        /// </summary>
        /// <param name="optionsBuilder"></param>
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
                optionsBuilder.UseSqlServer(ConnectionString);
            }
        }

        /// <summary>
        /// 配置数据表的映射关系
        /// </summary>
        /// <param name="modelBuilder"></param>
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<UserEntity>(entity =>
            {
                entity.ToTable("UserEntity")
                .HasKey(e => e.UserId);
            });
        }
    }
}
